package com.bee.admin.modular.main.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.context.request.ServletWebRequest;

import com.bee.admin.common.Const;
import com.bee.admin.modular.main.service.IndexPageService;

@Controller
public class PageController {
	
	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	 
	@Autowired
	private IndexPageService service;
	
	
	@GetMapping(Const.DEFAULT_UNAUTHENTICATION_URL)
	public String require(Model model){
		model.addAttribute("data", service.getIndexVo());
		return "/index";
	}
	
	@GetMapping("/")
	public String index(Model model){
		model.addAttribute("data", service.getIndexVo());
		return "/login";
	}
		@GetMapping("/login")
	public String login(HttpServletRequest request,Model model) {
	//	Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
	//	model.addAttribute("data", service.getIndexVo());
		return "/login_page";
	}
	
	
	@GetMapping("/register")
	public String register() {
		return "/detail";
	}
	
	@PostMapping("/form/login")
	public String formLogin(String username, String password, Model model) 
	{
		System.out.println("username:"+ username + " -passwrod: "+password);
		model.addAttribute("data", service.getIndexVo());
		 Authentication request = new UsernamePasswordAuthenticationToken(username, password);
		SecurityContextHolder.getContext().setAuthentication(request);
		return "/index";
	}
	
	
	@GetMapping("/detail")
	public String detail() {
		return "detail";
	}
	
}
